package cn.com.dhcc.ddi.node.dao;

import java.util.List;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;
import cn.com.dhcc.app.core.dao.BaseDao;
import cn.com.dhcc.app.core.exception.DaoException;
import cn.com.dhcc.app.core.service.Log;
import cn.com.dhcc.app.pub.CM;
import cn.com.dhcc.ddi.node.vo.OrgInfo;
import cn.com.dhcc.ddi.node.vo.wapper.OrgInfoRowWrapper;
/**
 * 
    * @ClassName: OrgInfoDao
    * @Description: 地区编码DAO
    * @author 18315
    * @date 2017-3-10
    *
 */
@Repository
public class OrgInfoDao extends BaseDao {
	private static Logger logger = Log.getLogger(Log.LOGTYPE.DDI);
	
	/**
	 * 根据节点编码查询区域列表
	 * @param code 
	 * @return List<{@link NodeRegion}>
	 * @throws DaoException 
	 */
	public List<OrgInfo> getOrgInfoList(String code) throws DaoException {
		String sql = "SELECT * FROM " + SI_TABLE("t_ddi_node_region") + " WHERE 1 = 1"
				+ " AND ad_cd LIKE '" + code + "%'";
		try {
			return CM.getDao().list("获取节点区域列表", sql, new OrgInfoRowWrapper(), new Object[]{});
		} catch (DaoException e) {
			logger.error("获取节点区域列表失败", e);
			throw new DaoException("获取节点区域列表失败", e);
		}
	}
	
	/**
	 * 获取行政区
	 * @param orgCode　行政区编码
	 * @return
	 * @throws DaoException
	 */
	public List<OrgInfo> getOrgCode() throws DaoException {
		String msg = "";
		String sql="select  ad_cd,ad_nm"
				   +" from "+ SI_TABLE("t_ddi_node_region"); 
				  
		try {
			return CM.getDao().list("获取行政区列表", sql,
					new OrgInfoRowWrapper(), new Object[] {});
		} catch (DaoException e) {
			msg = "获取列表失败";
			logger.error("获取列表失败", e);
			throw new DaoException(msg, e);
		}
	}
	
	/**
	 * 获取行政区(备用)
	 * @param NodeCode
	 * @return
	 * @throws DaoException
	 */
	public OrgInfo get(String nodeCode)throws DaoException {
		String sql = "select * from "+ SI_TABLE("t_ddi_node_region") +" t  where "
				+ " t.ad_cd =? ";
		try {
			return CM.getDao().get("获取行政区", sql, new OrgInfoRowWrapper(),	new Object[] {nodeCode});
		} catch (DaoException e) {
			logger.error("获取行政区失败", e);
			throw new DaoException("获取行政区失败", e);
		}
	}
	
	
	
	/**
	 * 根据节点编码获取节点信息
	 * @param code 节点编码
	 * @return 
	 * @throws DaoException 
	 */
	public OrgInfo getOrgInfo(String nodeCode) throws DaoException {
		String sql = "SELECT * FROM " + SI_TABLE("t_ddi_node_region") + " WHERE ad_cd = ?";
		try {
			return CM.getDao().get("", sql, new OrgInfoRowWrapper(), new Object[]{nodeCode});
		} catch (DaoException e) {
			logger.error("获取列表失败", e);
			throw new DaoException("获取列表失败", e);
		}
	}
	

}
